/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution482 {

    public static void main(String args[]) {
        Solution482 s = new Solution482();
        System.out.println(s.licenseKeyFormatting("2-4A0r7-4k", 3));
    }

    public String licenseKeyFormatting(String S, int K) {
        String ns = S.toUpperCase();
        StringBuilder sb = new StringBuilder();

        int sum = 0;
        for(int i = ns.length() - 1; i >= 0; i --){
            if(ns.charAt(i) == '-'){
                continue;
            }
            if(sum == K){
                sb.append('-');
                sum = 0;
            }
            if(ns.charAt(i) != '-'){
                sum ++;
                sb.append(ns.charAt(i));
            }

        }
        return sb.reverse().toString();
    }
}
